﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using FacturIT.BLL.Model.Project;
using FacturIT.BLL.Model.Helper;
using System.Data.Linq;
using FacturIT.Service.Repositories.userCompany;


namespace FacturIT.Service.Repositories.Project
{
    public class ProjectRepository : IRepository<BLL.Model.Project.Project, int>
    {
        public ProjectRepository()
        {
            DB = new ProjectManagementDataContext(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DatabaseToUse"]].ConnectionString);
        }

        protected Table<BLL.Model.Project.Project> DT
        {
            get { return ((ProjectManagementDataContext)base.DB).Projects; }
        }

        public override BLL.Model.Project.Project Read(int id)
        {
            BLL.Model.Project.Project project = ReadByID(id);
            return project == null ? null : project;
        }
        public BLL.Model.Project.Project Read(string name)
        {
            return (from project in DT
                    where project.Name.ToLower().Equals(name.ToLower())
                    select project).FirstOrDefault();
        }

        public override IEnumerable<BLL.Model.Project.Project> ReadAll()
        {
            return from project in DT
                   orderby project.Name
                   select project;
        }

        public override IEnumerable<BLL.Model.Project.Project> ReadAll(params int[] ids)
        {
            return from project in DT
                   where ids.Contains(project.ProjectID)
                   select project;
        }
        internal IEnumerable<BLL.Model.Project.Project> ReadAll(List<int> assetIDs)
        {
            return from project in DT
                   where assetIDs.Contains(project.ProjectID)
                   select project;
        }
        

        public override BLL.Model.Project.Project Add(BLL.Model.Project.Project t)
        {
            DT.InsertOnSubmit(t);
            SaveChanges();
            return ReadByID(t.ProjectID);
        }

        public override bool Delete(BLL.Model.Project.Project t)
        {
            DT.DeleteOnSubmit(t);
            SaveChanges();
            return !Exists(t.ProjectID);
        }

        public override BLL.Model.Project.Project Update(BLL.Model.Project.Project t)
        {
            UpdateObject(ReadByID(t.ProjectID), t, false);
            SaveChanges();
            return ReadByID(t.ProjectID);
        }

        public override BLL.Model.Project.Project Duplicate(BLL.Model.Project.Project t)
        {
            return Add(Create(t.Name, t.StartDate, t.EndData, t.Active));
        }

        protected override BLL.Model.Project.Project ReadByID(int id)
        {
            return DT.SingleOrDefault(x => x.ProjectID == id);
        }

        protected override void UpdateObject(BLL.Model.Project.Project left, BLL.Model.Project.Project right, bool replaceID)
        {
            if (replaceID) left.ProjectID = right.ProjectID;
            left.Name = right.Name;
            left.StartDate = right.StartDate;
            left.EndData = right.EndData;
            
        }

        protected override IEnumerable<BLL.Model.Project.Project> ApplyOrdering(IEnumerable<BLL.Model.Project.Project> query, IDictionary<Func<BLL.Model.Project.Project, object>, Enum.Ordering> columns)
        {
            return OrderHelper.ApplyOrdering<BLL.Model.Project.Project, object>(query, columns, project => project.ProjectID);
        }

        protected BLL.Model.Project.Project Create(string name, DateTime startdate, DateTime? enddate, bool active)
        {
            return new BLL.Model.Project.Project()
            {
                Name = name,
                StartDate = startdate,
                EndData = enddate,
                Active = active
            };
        }

        internal IEnumerable<BLL.Model.Project.Project> ReadAllProjectsFromCompany(int CompanyID)
        {
            return from project in DT
                   where project.CompanyProjects.Select(f => f.CompanyID).FirstOrDefault() == CompanyID
                   select project;
        }

        internal IEnumerable<BLL.Model.Project.Project> ReadProjectByID(int projectid)
        {
            return from project in DT
                   where project.ProjectID == projectid
                   select project;
        }

        internal BLL.Model.Project.Project GetProjectByValues(string name, DateTime start, DateTime? end, bool p, int paymenttype, int customer, int company)
        {
            return (from project in DT
                   where project.Name == name && project.StartDate == start && project.EndData == end && project.Active == p && project.PaymentType == paymenttype
                   && project.Customer == customer && project.Company == company
                   select project).FirstOrDefault();
        }

        internal BLL.Model.Project.Project ReadPaymentTypeFromProject(int projectID)
        {
            return (from project in DT
                   where project.ProjectID == projectID
                   select project).FirstOrDefault();
        }

        internal IEnumerable<BLL.Model.Project.Project> ReadAllActiveProjectsFromCompany(int companyid)
        {
            return from project in DT
                   where project.CompanyProjects.Select(f => f.CompanyID).FirstOrDefault() == companyid && project.Active == true
                   select project;
        }
    }
}